﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using TSqlServer;
public partial class log_LogShow : System.Web.UI.Page
{
    public string nextid;
    public string perid;
    public string sum;
    public string num="1";
    public string content;
    public string title;
    public string intime;
    public string logid;
    public string img;
    public SqlDataReader dr;
    protected void Page_Load(object sender, EventArgs e)
    {
       
            if (Request["operation"] != null && Request["id"] != null)
            {
                switch (Request["operation"].ToString())
                {
                    case "show":
                        Show(Request["id"].ToString());
                        break;
                    case "del":
                        Del(Request["id"].ToString());
                        break;
                }
            }
        
    }
    public void Show(string id)
    {
        string sSql ;
        if (id == "")
        {
            sSql = "select distinct row_number() over(order by intime desc) as num,(select count(*) from t_log_info where status=1 and (((select authority from t_log_type where id=a.typeid)=1  and a.authority=0) or a.authority=1)  or inman='" + Session["userid"].ToString() + "') as [rowcount] ,a.*,'..\\user\\pics\\' + isnull(b.mypic ,'user.jpg') as img from t_log_info as a  left join t_user as b on a.inman=b.email  where a.status=1 and (((select authority from t_log_type where id=a.typeid)=1  and a.authority=0) or a.authority=1) or a.inman='" + Session["userid"].ToString() + "'";
        }
        else
        {
            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_log_info where status=1 and (((select authority from t_log_type where id=a.typeid)=1  and a.authority=0) or a.authority=1)  or inman='" + Session["userid"].ToString() + "') as [rowcount] ,a.*,'..\\user\\pics\\' + isnull(b.mypic ,'user.jpg') as img  from t_log_info as a left join t_user as b on a.inman=b.email  where a.status=1 and (((select authority from t_log_type where id=a.typeid)=1  and a.authority=0) or a.authority=1) or a.inman='" + Session["userid"].ToString() + "' ) " +
                   "select distinct * from a where id='" + id + "' ";
        }
        TSqlDBReader myReader = new TSqlDBReader(sSql);
        dr = myReader.FDataReader;
        dr.Read();

        title = dr["title"].ToString();
        intime = dr["intime"].ToString();
        logid = dr["id"].ToString();
        content = dr["content"].ToString();
        sum = dr["rowcount"].ToString();
        num = dr["num"].ToString();
        img = dr["img"].ToString();
        if (dr["inman"].ToString() != Session["userid"].ToString())
            sp1.Visible = false;
        if ( num == "1")
        {

            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') as [rowcount] ,* from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') " +
                   "select id from a where num='" + sum + "' ";
            myReader = new TSqlDBReader(sSql);
            dr = myReader.FDataReader;
            dr.Read();
            perid ="logshow.aspx?operation=show&id="+ dr["id"].ToString();
            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') as [rowcount] ,* from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') " +
                  "select id from a where num='" + (int.Parse(num) + 1) + "' ";
            myReader = new TSqlDBReader(sSql);
            dr = myReader.FDataReader;
            dr.Read();
            if (dr.HasRows )
                nextid = "logshow.aspx?operation=show&id=" + dr["id"].ToString();
            else
                nextid = "javascript:void(0)";
                

        }
        else if (num == sum)
        {

            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') as [rowcount] ,* from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') " +
                   "select id from a where num='" + (int.Parse(num) - 1) + "' ";
            myReader = new TSqlDBReader(sSql);
            dr = myReader.FDataReader;
            dr.Read();
            perid ="logshow.aspx?operation=show&id="+ dr["id"].ToString();
            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') as [rowcount] ,* from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') " +
                  "select id from a where num='" + 1 + "' ";
            myReader = new TSqlDBReader(sSql);
            dr = myReader.FDataReader;
            dr.Read();
            nextid ="logshow.aspx?operation=show&id=" + dr["id"].ToString();

        }
        else
        {
            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') as [rowcount] ,* from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') " +
                   "select id from a where num='" + (int.Parse(num) - 1) + "' ";
            myReader = new TSqlDBReader(sSql);
            dr = myReader.FDataReader;
            dr.Read();
            perid = "logshow.aspx?operation=show&id="+dr["id"].ToString();
            sSql = "with a as ( select row_number() over(order by intime desc) as num,(select count(*) from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') as [rowcount] ,* from t_log_info where status=1  or inman='" + Session["userid"].ToString() + "') " +
                  "select id from a where num='" + (int.Parse(num) + 1) + "' ";
            myReader = new TSqlDBReader(sSql);
            dr = myReader.FDataReader;
            dr.Read();
            nextid ="logshow.aspx?operation=show&id=" +dr["id"].ToString();
        }
        dr.Close();
        myReader.Free();
    }
    public void Del(string id)
    {
        TLogInfo.DeleteByID(id);
        Server.Transfer("logshow.aspx?operation=show&id=");
    }
}
